package com.bua.control;

import java.util.ArrayList;
import java.util.HashMap;

import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.widget.*;
import com.bua.net.Tool;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.bua.toothfactory.HuodongOrderActivity;

public class HuodongPaint extends LinearLayout {

	private GridView toothGallery;
    class Tooth
    {
        public  int x;
        public int y;
        public boolean isHave;
        public Tooth(int X,int Y,boolean IsHave)
        {
            x=X;
            y=Y;
            isHave=IsHave;

        }

    }
	private HashMap<Integer, Tooth> tooths = new HashMap<Integer, Tooth>();
    ImageView toothView;
    PaintView paintView  ;
    Bitmap backgroud;
    public HuodongOrderActivity act;

	void CreateTooth(Context context, int tag, int left, int top/*, OnToothClick l*/) {
//		IrregularButton button = new IrregularButton(context);
//		button.setBackgroundColor(Color.TRANSPARENT);//
//		button.setTag(tag);
		Bitmap bm = BitmapFactory.decodeStream(Tool.getFile(context, "hd/"
				+ String.valueOf(tag), "nonmal.png"));
        Canvas canvas = new Canvas(backgroud);
        canvas.drawBitmap( bm, left, top, null );//插入图标
        canvas.save(Canvas. ALL_SAVE_FLAG);
        canvas.restore();

        int width = bm.getWidth(); // 获取位图的宽
        int height = bm.getHeight(); // 获取位图的高

        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组

        bm.getPixels(pixels, 0, width, 0, 0, width, height);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if ((pixels[width * i + j] & 0xFF000000) != 0)
                    pixels[width * i + j] = 0xFF000000+tag;
                else
                    pixels[width * i + j] = 0x00000000;
            }
        }
        Bitmap dstBitmap = Bitmap.createBitmap(width, height,
                Config.ARGB_8888);
        dstBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        Canvas canvas2 = new Canvas(paintView.areaBitmap);
        canvas2.drawBitmap( dstBitmap, left, top, null );//插入图标

        canvas2.save(Canvas. ALL_SAVE_FLAG);
        canvas2.restore();

		this.tooths.put(tag, new Tooth(left,top,true));
//		toothView.addView(button);
	}

	public HuodongPaint(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setOrientation(HORIZONTAL);
		this.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        paintView= new PaintView(context);
        paintView.areaBitmap =  Bitmap.createBitmap( 397, 600, Config.ARGB_8888 );
		toothView = new ImageView(context) ;

        toothView.setDrawingCacheEnabled(true);
        toothView.setBackgroundColor(Color.WHITE);
        backgroud = Bitmap.createBitmap( 397, 600, Config.ARGB_8888 );
		FrameLayout paintArea = new FrameLayout(context);
		paintArea.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		paintArea.addView(toothView);
		paintArea.addView(paintView);
		this.addView(paintArea);
		CreateTooth(context, 21, 152, 9);
		CreateTooth(context, 22, 124, 20);
		CreateTooth(context, 23, 99, 30);
		CreateTooth(context, 24, 69, 59);
		CreateTooth(context, 25, 46, 93);
		CreateTooth(context, 26, 28, 131);
		CreateTooth(context, 27, 20, 172);
		CreateTooth(context, 28, 15, 216);

		CreateTooth(context, 11, 192, 9);
		CreateTooth(context, 12, 228, 20);
		CreateTooth(context, 13, 253, 30);
		CreateTooth(context, 14, 269, 59);
		CreateTooth(context, 15, 297, 93);
		CreateTooth(context, 16, 307, 131);
		CreateTooth(context, 17, 315, 172);
		CreateTooth(context, 18, 312, 216);

		CreateTooth(context, 31, 173, 546);
		CreateTooth(context, 32, 140, 542);
		CreateTooth(context, 33, 115, 525);
		CreateTooth(context, 34, 85, 505);
		CreateTooth(context, 35, 62, 473);
		CreateTooth(context, 36, 35, 428);
		CreateTooth(context, 37, 20, 378);
		CreateTooth(context, 38, 13, 321);

		CreateTooth(context, 41, 203, 546);
		CreateTooth(context, 42, 230, 542);
		CreateTooth(context, 43, 254, 525);
		CreateTooth(context, 44, 271, 505);
		CreateTooth(context, 45, 299, 473);
		CreateTooth(context, 46, 310, 428);
		CreateTooth(context, 47, 325, 378);
		CreateTooth(context, 48, 319, 321);
        toothView.setImageBitmap(backgroud);
        paintView.SetClickListener(onClick);

    }
    OnImageClickListener onClick =new OnImageClickListener() {
        @Override
        public void onClick(int tag) {
            //To change body of implemented methods use File | Settings | File Templates.
           // setToothImage( view.getTag().toString());
            act.setAdapter(tag);
        }
    };
    public void setToothViewStyle(int type)
    {
        paintView.setStyle(type);
    }
    public void setToothImage(String tag)
    {
//        Tool.getFileList()
//        Tooth ibTooth= tooths.get(tag);
//        if(ibTooth==null)
//            return;
//        Bitmap bm = BitmapFactory.decodeStream(Tool.getFile(this.getContext(), "hd",
//                "t" + tag, "png"));
//
//        int width = bm.getWidth(); // 获取位图的宽
//        int height = bm.getHeight(); // 获取位图的高
//
//        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
//
//        bm.getPixels(pixels, 0, width, 0, 0, width, height);
//        for (int i = 0; i < height; i++) {
//            for (int j = 0; j < width; j++) {
//                if ((pixels[width * i + j] & 0xFF000000) != 0)
//                    pixels[width * i + j] = 0xFFFFFFFF;
//                else
//                    pixels[width * i + j] = 0x00FFFFFF;
//            }
//        }
//        Bitmap dstBitmap = Bitmap.createBitmap(width, height,
//                Config.ARGB_8888);
//        dstBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
//
//
//        Canvas canvas = new Canvas(backgroud);
//        ibTooth.isHave=!ibTooth.isHave;
//        if(ibTooth.isHave)
//            canvas.drawBitmap( bm, ibTooth.x, ibTooth.y, null );//插入图标
//        else
//            canvas.drawBitmap( dstBitmap, ibTooth.x, ibTooth.y, null );//插入图标
//
//        canvas.save(Canvas. ALL_SAVE_FLAG);
//        canvas.restore();
//        setToothsAdapter();
    }


    public void DrawTooth(int tag,String path)
    {
        Bitmap toothImg=BitmapFactory.decodeStream(Tool.getFile(act.getBaseContext(),path));
        Tooth tooth=  tooths.get(tag);
        paintView.DrawImage(toothImg,tooth.x,tooth.y);
    }
}
